<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_daftar_ulang extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function get_calon_siswa($id){
		$this->db->flush_cache();
		$this->db->select('a.*, b.nama_psb, c.nama_kelompok, a.status');
		$this->db->from('tbl_sd_calonsiswa a');
		$this->db->join('tbl_sd_psb b', 'b.id_psb=a.id_psb');
		$this->db->join('tbl_sd_psb_kelompok c', 'c.id_kelompok=a.id_kelompok');
		$this->db->where('id', $id);
		
		return $this->db->get();
	}
	
	function save($data){
		$this->db->trans_start();
		
		// update berkas di tbl calon siswa persyaratan
		for($i=0; $i<count($data['persyaratan']); $i++){
			$this->db->flush_cache();
			$this->db->set('ceklis', isset($data['chk_persyaratan'][$i])?'1':'0');
			$this->db->where('id', $data['persyaratan'][$i]['id']);
			
			$this->db->update('tbl_sd_calonsiswa_persyaratan');
		}
		
		// update tbl calon siswa => tanggal dan status-nya jadi 4 (telah daftar ulang/terdaftar)
		$this->db->flush_cache();
		$this->db->set('tgl_daftar_ulang', date('Y-m-d'));
		$this->db->set('status', '4');
		$this->db->where('id', $data['id']);
		$this->db->update('tbl_sd_calonsiswa');
		
		// insert to tbl sd siswa
		$calon = $data['calonsiswa'];
		$this->db->flush_cache();
        $this->db->set('id_calonsiswa', $data['id']);
        $this->db->set('nis', $data['nis']);
        $this->db->set('id_kelas', '');
        $this->db->set('tahun_masuk', $calon->row()->tahun_masuk);
        $this->db->set('nama', $calon->row()->nama);
        $this->db->set('jk', $calon->row()->jk);
        $this->db->set('tmp_lahir', $calon->row()->tmp_lahir);
        $this->db->set('tgl_lahir', $calon->row()->tgl_lahir);
        $this->db->set('Agama', $calon->row()->Agama);
        $this->db->set('kewarganegaraan', $calon->row()->kewarganegaraan);
        $this->db->set('anakke', $calon->row()->anakke);
        $this->db->set('jml_saudara', $calon->row()->jml_saudara);
        $this->db->set('bahasa', $calon->row()->bahasa);
        $this->db->set('foto', $calon->row()->foto);
        $this->db->set('alamat', $calon->row()->alamat);
        $this->db->set('tlp', $calon->row()->tlp);
        $this->db->set('asal_sekolah', $calon->row()->asal_sekolah);
        $this->db->set('gol_darah', $calon->row()->gol_darah);
        $this->db->set('riwayat_penyakit', $calon->row()->riwayat_penyakit);
        $this->db->set('nama_ayah', $calon->row()->nama_ayah);
        $this->db->set('nama_ibu', $calon->row()->nama_ibu);
        $this->db->set('status_ayah', $calon->row()->status_ayah);
        $this->db->set('status_ibu', $calon->row()->status_ibu);
        $this->db->set('alm_ayah', $calon->row()->alm_ayah);
        $this->db->set('alm_ibu', $calon->row()->alm_ibu);
        $this->db->set('tmp_lahir_ayah', $calon->row()->tmp_lahir_ayah);
        $this->db->set('tmp_lahir_ibu', $calon->row()->tmp_lahir_ibu);
        $this->db->set('tgl_lahir_ayah', $calon->row()->tgl_lahir_ayah);
        $this->db->set('tgl_lahir_ibu', $calon->row()->tgl_lahir_ibu);
        $this->db->set('pendidikan_ayah', $calon->row()->pendidikan_ayah);
        $this->db->set('pendidikan_ibu', $calon->row()->pendidikan_ibu);
        $this->db->set('pekerjaan_ayah', $calon->row()->pekerjaan_ayah);
        $this->db->set('pekerjaan_ibu', $calon->row()->pekerjaan_ibu);
        $this->db->set('penghasilan_ayah', $calon->row()->penghasilan_ayah);
        $this->db->set('penghasilan_ibu', $calon->row()->penghasilan_ibu);
        $this->db->set('nama_wali', $calon->row()->nama_wali);
        $this->db->set('alamat_ortu', $calon->row()->alamat_ortu);
        $this->db->set('tlp_ortu', $calon->row()->tlp_ortu);
        $this->db->set('status', '1');
		
		$this->db->insert('tbl_sd_siswa');
		
		
		// insert to keuangan
		// belum ada tabel nya.....
		
		//return
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}
	
	function get_calonsiswa_by_id($id){
		$this->db->flush_cache();
		$this->db->select('id, no_pendaftaran, nama, id_psb, id_kelompok, tahun_masuk, uang_sumbangan, tgl_lahir, tmp_lahir, tlp, alamat');
		$this->db->where('id', $id);
		return $this->db->get('tbl_sd_calonsiswa');
	}
	
	function generate_nis(){
		// get setting nis
		$this->db->flush_cache();
		$this->db->where('status', '1');
		$res = $this->db->get('tbl_sd_psb');
		
		$awalan = $res->row()->nis_awalan;
		$digit = $res->row()->nis_digit;
		
		$str_like = str_pad($awalan, ($digit+strlen($awalan)), '_'); // return ###___
		
		// get last nis
		$this->db->flush_cache();
		$this->db->where("nis LIKE '$str_like'");
		$this->db->order_by('nis', 'DESC');
		$this->db->limit('1');
		$res = $this->db->get('tbl_sd_siswa');
		
		// proses generate nis
		$nomor = 1;
		if($res->num_rows()>0){
			$tmp_num = substr($res->row()->nis, strlen($awalan));
			$nomor = $tmp_num + 1;
		}
		
		$tmp_nis = str_pad($nomor, $digit, '0', STR_PAD_LEFT); // return ##### => 00001
		
		return $awalan.$tmp_nis;
		
	}
	
	function nis_exist($nis){
		$this->db->flush_cache();
		$this->db->where('nis', $nis);
		
		return ($this->db->get('tbl_sd_siswa')->num_rows() > 0);
	}
	
	function get_setting_pembayaran(){
		$this->db->flush_cache();
		$this->db->order_by('posisi', 'ASC');
		
		return $this->db->get('tbl_ref_sd_pembayaran_psb');
	}
	
}

?>